package com.dsj.gxrd.modules.controller.people;

import com.dsj.gxrd.modules.people.domain.dto.RdUserInfoDto;
import com.dsj.gxrd.modules.people.domain.query.RdUserInfoQuery;
import com.dsj.gxrd.modules.people.domain.vo.RdUserInfoVo;
import com.dsj.gxrd.modules.people.service.RdUserInfoService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.RemoteUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author songpin566
 * @date 2023/09/21
 **/
@Api(tags = "机构人员管理")
@RestController
@RequestMapping("/people/manage")
public class RdUserInfoController extends BaseController {

    @Autowired
    private RdUserInfoService userInfoService;

    @Autowired
    private RemoteUserService remoteUserService;

    @RequiresPermissions("people:manage:list")
    @ApiOperation(value = "查询机构人员信息", response = RdUserInfoVo.class)
    @GetMapping("/list")
    public TableDataInfo list(RdUserInfoQuery userInfoQuery) {
        startPage();
        List<RdUserInfoVo> list = userInfoService.list(userInfoQuery);
        return getDataTable(list);
    }

    @RequiresPermissions("people:manage:add")
    @ApiOperation(value = "新增机构人员信息")
    @PostMapping()
    public AjaxResult add(@RequestBody RdUserInfoDto userInfoDto) {
        return userInfoService.add(userInfoDto);
    }

    @RequiresPermissions("people:manage:edit")
    @ApiOperation(value = "修改机构人员信息")
    @PutMapping()
    public AjaxResult edit(@RequestBody RdUserInfoDto userInfoDto) {
        return userInfoService.edit(userInfoDto);
    }

    @RequiresPermissions("people:manage:del")
    @ApiOperation(value = "删除机构人员信息")
    @DeleteMapping("/{userIds}")
    public AjaxResult removes(@PathVariable List<Long> userIds) {
        return userInfoService.removes(userIds);
    }

    @RequiresPermissions("people:manage:query")
    @ApiOperation(value = "查询机构人员信息详情")
    @GetMapping("/{userId}")
    public AjaxResult getInfo(@PathVariable Long userId) {
        AjaxResult result = remoteUserService.getInfo(userId);
        if ((int) result.get(AjaxResult.CODE_TAG) == Constants.FAIL) {
            throw new ServiceException("获取用户信息失败!" + result.get(AjaxResult.MSG_TAG));
        }
        RdUserInfoVo sysUser = userInfoService.selectUserById(userId);
        result.put("data", sysUser);
        return result;
    }


}
